package com.xcm.service.impl;

import com.xcm.cache.RedisCacheDao;
import com.xcm.constant.BusinessConstant;
import com.xcm.constant.CacheConstant;
import com.xcm.model.*;
import com.xcm.model.bo.ClientDeviceBO;
import com.xcm.model.dto.NewestProgramDTO;
import com.xcm.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户端与服务端交互逻辑Service实现类
 */
@Service(value = "clientService")
public class ClientServiceImpl implements ClientService {
	@Autowired
	private RedisCacheDao redisCacheDao;
	@Autowired
	private DeviceInfoService deviceInfoService;
	@Autowired
	private ProgramInfoService programInfoService;
	@Autowired
	private ProgramTopService programTopService;
	@Autowired
	private ProgramBottomService programBottomService;
	@Autowired
	private ProgramJsonService programJsonService;
	@Autowired
	private DeviceUpgradeService deviceUpgradeService;
	@Autowired
	private ProgramDownloadService programDownloadService;
	@Autowired
	private DeviceLogService deviceLogService;

	/**
	 * 获取最新设置数据
	 *
	 * @param mac     mac地址
	 * @param version 版本号
	 * @return ClientDeviceBO
	 */
	@Override
	public ClientDeviceBO getStates(String mac, String version, TbDeviceInfo deviceInfo) {
		//上线判断
		Object cache = redisCacheDao.getCache(CacheConstant.CACHE_NAME_DEVICE_ONLINE, mac);
		//没有登陆数据
		if (cache == null) {
			//设置上线
			deviceInfo.setDeviceStatus(0);
			deviceInfoService.updateDeviceInfo(deviceInfo, deviceInfo);
		}
		//放入缓存
		redisCacheDao.putCache(CacheConstant.CACHE_NAME_DEVICE_ONLINE, mac, System.currentTimeMillis() / 1000);
		ClientDeviceBO resultClientDeviceBO = new ClientDeviceBO();
		resultClientDeviceBO.setDeviceNum(deviceInfo.getDeviceNum());
		resultClientDeviceBO.setDeviceVolume(deviceInfo.getDeviceVolume());
		resultClientDeviceBO.setUseTimer(deviceInfo.getUseTimer());
		//使用了定时开关机
		if (1 == deviceInfo.getUseTimer()) {
			resultClientDeviceBO.setBootTime(deviceInfo.getBootTime());
			resultClientDeviceBO.setOffTime(deviceInfo.getOffTime());
		}
		//判断是否有开机关机设置
		ClientDeviceBO optionClientDevice = (ClientDeviceBO) redisCacheDao.getCache(CacheConstant.CACHE_NAME_DEVICE_OPTION, mac);
		resultClientDeviceBO.setOff(false);
		resultClientDeviceBO.setRestart(false);
		resultClientDeviceBO.setLog(false);
		if (optionClientDevice != null) {
			if (optionClientDevice.getOff() != null) {
				resultClientDeviceBO.setOff(optionClientDevice.getOff());
			}
			if (optionClientDevice.getRestart() != null) {
				resultClientDeviceBO.setRestart(optionClientDevice.getRestart());
			}
			if (optionClientDevice.getLog() != null) {
				resultClientDeviceBO.setLog(optionClientDevice.getLog());
			}
			redisCacheDao.deleteByKey(CacheConstant.CACHE_NAME_DEVICE_OPTION, mac);
		}
		//通过机器码查询节目信息
		TbProgramInfo programInfo = programInfoService.getProgramInfoByMachineNum(deviceInfo.getDeviceNum());
		
		return resultClientDeviceBO;
	}

	/**
	 * 获取最新广告数据
	 *
	 * @param mac              mac地址
	 * @param version          xc版本号
	 * @param newestProgramDTO NewestProgramDTO对象
	 * @return String(提示信息)
	 */
	@Override
	public String getNewestProgram(String mac, String version, NewestProgramDTO newestProgramDTO) {
		String msg = "";
		//查找终端信息
		TbDeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByMac(mac);
		if (deviceInfo == null) {
			msg = "终端信息不存在";
		}
		//查找节目信息
		String deviceNum = deviceInfo.getDeviceNum();
		TbProgramInfo programInfo = programInfoService.getProgramInfoByMachineNum(deviceNum);
		if (programInfo == null) {
			msg = "数据未更新";
		}
		String xcVersion = programInfo.getXcVersion();
		if (StringUtils.isNotBlank(version) && version.equals(xcVersion)) {
			msg = "数据未更新";
		}
		//下屏节目列表
		List<TbProgramBottom> bottomList = programBottomService.listProgramBottomByXcVersion(xcVersion, programInfo.getBottom());
		//上屏节目列表
		List<TbProgramTop> topList = programTopService.listProgramTopByXcVersion(xcVersion, programInfo.getTop());
		//节目Json
		TbProgramJson programJson = programJsonService.getProgramJsonByXcVersion(xcVersion);
		newestProgramDTO.setXcVersion(version);
		newestProgramDTO.setJsonName(programJson.getName());
		newestProgramDTO.setTopProgramList(topList);
		newestProgramDTO.setBottomProgramList(bottomList);
		return msg;
	}

	/**
	 * 获取终端升级
	 *
	 * @param model 对应机型
	 * @param mac   mac地址
	 * @return TbDeviceUpgrade
	 */
	@Override
	public TbDeviceUpgrade getDeviceUpgrade(String model, String mac) {
		//通过对应机型获取终端升级
		TbDeviceUpgrade deviceUpgradeModel = deviceUpgradeService.getDeviceUpgradeByModel(model);
		if (StringUtils.isNotBlank(mac)) {
			//通过mac取终端升级
			TbDeviceUpgrade deviceUpgradeMac = deviceUpgradeService.getDeviceUpgradeByMac(mac);
			if (deviceUpgradeModel == null) {
				deviceUpgradeModel = deviceUpgradeMac;
			} else {
				if (deviceUpgradeMac != null) {
					//通过Mac查询出的终端升级的更新时间大于通过对应机型查询出的终端升级的更新时间
					if (deviceUpgradeMac.getUpdateTime() > deviceUpgradeModel.getUpdateTime()) {
						deviceUpgradeModel = deviceUpgradeMac;
					}
				}
			}
		}
		return deviceUpgradeModel;
	}

	/**
	 * 上报广告数据的下载状态
	 *
	 * @param mac          mac地址
	 * @param playJson     当前播放Json
	 * @param downloadJson 当前下载
	 * @param state        下载状态
	 */
	@Override
	public void setDownload(String mac, String playJson, String downloadJson, Integer state) {
		//通过mac地址获取TbProgramDownload
		TbProgramDownload programDownload = programDownloadService.getProgramDownloadByMac(mac);
		//如果为空就保存，否则，就更新
		if (programDownload == null) {
			programDownload = new TbProgramDownload();
			programDownload.setDeviceMac(mac);
			programDownload.setPlayJson(playJson);
			programDownload.setDownloadJson(downloadJson);
			programDownload.setUpdateTime(System.currentTimeMillis());
			programDownload.setState(state);
			programDownloadService.saveProgramDownload(programDownload);
		} else {
			programDownload.setPlayJson(playJson);
			programDownload.setDownloadJson(downloadJson);
			programDownload.setState(state);
			programDownload.setUpdateTime(System.currentTimeMillis());
			programDownloadService.updateProgramDownload(programDownload);
		}
	}

	/**
	 * 上传日志
	 * @param mac   mac地址
	 * @param name  压缩包名
	 * @param url   文件地址
	 */
	@Override
	public void uploadLog(String mac, String name, String url) {
		TbDeviceLog deviceLog = new TbDeviceLog();
		deviceLog.setMac(mac);
		deviceLog.setName(name);
		deviceLog.setUrl(url);
		deviceLog.setUploadTime(System.currentTimeMillis());
		deviceLogService.saveDeviceLog(deviceLog);
	}

	/**
	 * 设置机器码
	 *
	 * @param deviceMac 终端mac地址
	 * @param deviceNum 机器码
	 */
	@Override
	public void setDeviceNum(String deviceMac, String deviceNum) {
		//通过终端Mac地址查找终端信息
		TbDeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByMac(deviceMac);
		//如果存在终端信息,更新机器码
		if (deviceInfo != null) {
			deviceInfoService.updateDeviceNumByDeviceMac(deviceMac, deviceNum);
		}
	}

	/**
	 * 设置音量
	 *
	 * @param deviceMac    终端Macs地址
	 * @param deviceVolume 音量
	 */
	@Override
	public void setVolume(String deviceMac, Integer deviceVolume) {
		//通过终端Mac地址查找终端信息
		TbDeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByMac(deviceMac);
		//如果存在终端信息，修改终端音量
		if (deviceInfo != null) {
			deviceInfoService.updateDeviceVolumeByDeviceMac(deviceMac, deviceVolume);
		}
	}

	/**
	 * 设置定时开关机
	 *
	 * @param mac      mac地址
	 * @param useTimer 是否启用定时开关机
	 * @param bootTime 开机时间
	 * @param offTime  关机时间
	 */
	@Override
	public void setTimer(String mac, Integer useTimer, Long bootTime, Long offTime) {
		if (BusinessConstant.DEVICE_INFO_USE_TIMER_OFF == useTimer) {
			bootTime = null;
			offTime = null;
		}
		//查询终端信息
		TbDeviceInfo deviceInfo = deviceInfoService.getDeviceInfoByMac(mac);
		//如果存在终端信息，更新终端的定时开关机时间
		if (deviceInfo != null) {
			deviceInfoService.updateDeviceBootOffTimeByMac(mac, useTimer, bootTime, offTime);
		}
	}
}
